package blog.spring.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.core.exception.FormParamErrorException;
import blog.spring.db.entity.Type;
import blog.spring.db.service.IArticleService;
import blog.spring.db.service.ITypeService;
import blog.spring.db.service.IUserService;
import blog.spring.db.service.impl.TypeService.DescribeType;
import blog.spring.web.util.CookieUtils;

@Controller
@RequestMapping("/type")
public class TypeController extends BlogController {

	public static final String CURRENT_DIR = "type";

	public TypeController() {
		super(CURRENT_DIR);
	}

	@Resource
	private ITypeService typeService;
	@Resource
	private IArticleService articleService;
	@Resource
	private IUserService userService;

	@RequestMapping(value = { "", "*" })
	public String index(ModelMap map) {
		map.addAttribute("types",
				typeService.getTypeListAll(DescribeType.ARTICLE));
		return forwardCurentIndex();
	}

	/***************** 获得文章列表 *****************/
	@RequestMapping(value = "/{id}/articles", method = RequestMethod.GET)
	public String getArticles(@PathVariable("id") int id, ModelMap map) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("type.typeId", id);
		map.addAttribute("infos",
				articleService.searchList(conditions, 1, 10, null, null, false));
		return "article/list";
	}

	/***************** 新增类型 *****************/
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return forwardCurentAdd();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Type type, HttpServletRequest req, ModelMap map) {
		if (CookieUtils.isLogin(req, userService) == null)
			return forwardCurentAdd();
		try {
			typeService.save(type);
		} catch (FormParamErrorException e) {
			addException(map, e);
			return forwardCurentAdd();
		}
		return redirectIndex();
	}

	/***************** 修改类型 *****************/
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") int id, ModelMap map) {
		map.addAttribute("type", typeService.findById(id));
		return forwardCurentUpdate();
	}

	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	public String update(@PathVariable("id") int id, Type type,
			HttpServletRequest req, ModelMap map) {
		if (CookieUtils.isLogin(req, userService) == null)
			return forwardCurentUpdate();
		type.setTypeId(id);
		try {
			typeService.update(type);
		} catch (FormParamErrorException e) {
			addException(map, e);
			return forwardCurentUpdate();
		}
		return redirectIndex();
	}

	/***************** 删除类型 *****************/
	@RequestMapping(value = "/remove/{id}", method = RequestMethod.GET)
	public String remove(@PathVariable("id") int id, ModelMap map) {
		Type type = null;
		type = typeService.findById(id);
		if (type != null) {
			typeService.remove(type);
		}
		return redirectIndex();
	}
}
